<template>
  <div>
    <BasicTable @register="registerTable">
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'ant-design:cloud-download-outlined',
              label: '点击下载',
              onClick: handleDownload.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getLogFile, downloadLogFiles } from '/@/api/system/system';
  import { downloadByData } from '/@/utils/file/download';
  import { columns, searchFormSchema } from './log.data';
  import { dateUtil } from '/@/utils/dateUtil';

  export default defineComponent({
    name: 'DownloadLog',
    components: {
      BasicTable,
      TableAction,
    },
    setup() {
      const [registerTable, { reload }] = useTable({
        title: '日志下载',
        api: getLogFile,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        beforeFetch: (params) => {
          params.startTime = params.createTime ? dateUtil(params.createTime[0]).valueOf() : '';
          params.endTime = params.createTime ? dateUtil(params.createTime[1]).valueOf() : '';
          return params;
        },
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
      });

      function handleSuccess() {
        reload();
      }

      function handleDownload(record) {
        downloadLogFiles({ fileName: record.fileName }).then((blob) => {
          downloadByData(blob, record.fileName);
        });
      }

      return {
        registerTable,
        handleSuccess,
        handleDownload,
      };
    },
  });
</script>
